/**
 * @Author bian
 * @CreateDate 2017.3.2
 */
import { combineReducers } from 'redux'
import {GETCOURSES,FILTER, SORT, CHANGEPAGE } from "../../actions/user/course_list_actions"

require("../toastr")

const initialSate = {
    courses : [],
    cachedCourses : [],
    page : 1,
    limit : 12,
    totalPage : 1,
    gist : "time",   // time or popularity
    major : "ALL"
}

function userCourseList(state = initialSate,action){
    switch(action.type){
        case GETCOURSES :
            var newCourses = action.courses

            var totalPage = newCourses.length % state.limit ?
                parseInt(newCourses.length / state.limit) + 1 :
                newCourses.length / state.limit

            newCourses.sort((a,b) => {      // sort
                return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
            })
            return Object.assign({},state,{
                courses : newCourses.slice(0,state.limit),      // limit
                cachedCourses : newCourses,
                totalPage : totalPage
            })

        case FILTER:
            var major = action.major

            var newCourses = Array.prototype.concat.call([],state.cachedCourses)
            newCourses = newCourses.filter((e,i) => {       // filter

                return (e.major == major) || (major == "ALL")

            }).sort((a,b) => {                              // sort

                if(state.gist == "time"){
                    return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
                }else if(state.gist == "popularity"){
                    return new Date(b.starNum).getTime() - new Date(a.starNum).getTime()
                }

            })

            var totalPage = newCourses.length % state.limit ?
                parseInt(newCourses.length / state.limit) + 1 :
                newCourses.length / state.limit

            return Object.assign({},state,{     // 重置page和totalPage
                courses : newCourses.slice(0,state.limit),
                page : 1,
                totalPage,
                major
            })

        case SORT:
            var gist = action.gist
            var newCourses = Array.prototype.concat.call([],state.courses)
            if(gist == "time"){
                newCourses.sort((a,b) => {
                    return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
                })
            }else if(gist == "popularity"){
                newCourses.sort((a,b) => {
                    return new Date(b.starNum).getTime() - new Date(a.starNum).getTime()
                })
            }
            return Object.assign({},state,{
                courses : newCourses,
                gist : gist
            })

        case CHANGEPAGE:
            var page = action.page
            var newCourses = Array.prototype.concat.call([],state.cachedCourses)
            // filter
            newCourses = newCourses.filter((e,i) => {       // filter

                return (e.major == state.major) || (state.major== "ALL")

            })
            // sort
            if(gist == "time"){
                newCourses.sort((a,b) => {
                    return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
                })
            }else if(gist == "popularity"){
                newCourses.sort((a,b) => {
                    return new Date(b.starNum).getTime() - new Date(a.starNum).getTime()
                })
            }
            // change page
            newCourses = newCourses.slice((page - 1) * state.limit, page * state.limit)
            return Object.assign({},state,{
                courses : newCourses,
                page
            })

        default :
            return state
    }
}

const userCourseListApp = combineReducers({
    userCourseList
})

export default userCourseListApp